package bddaccess;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;

import modex.Document;

public class DocumentDAO extends DAO<Document> {

	@Override
	public Document create(Document obj) {
		try {
			int id = 0;
			PreparedStatement prepare = this.connect
			                            	.prepareStatement(
			                            			"INSERT INTO Document (idDocument, nomDocument) VALUES(?, ?)",
			                            			Statement.RETURN_GENERATED_KEYS
			                            	);
			prepare.setInt(1, id);
			prepare.setString(2, obj.getNomDocument());
			
			prepare.executeUpdate();
			
			ResultSet rs = prepare.getGeneratedKeys();
			if (rs.next()){
			    id=rs.getInt(1);
			}
			obj = this.find(id);

	    } catch (SQLException e) {
	            e.printStackTrace();
	    }
	    return obj;
	}
	
	public void createMultiple(ArrayList<Document> obj, int lot) {
		
		StringBuilder r = new StringBuilder();
		
		Iterator<Document> it = obj.iterator();
		
		while(it.hasNext()){
			r.append("INSERT INTO Document (idDocument, nomDocument) VALUES ");
			Document d = it.next();
			r.append("(").append(d.getIdDocument()).append(",'").append(d.getNomDocument()).append("')");
			for(int i=1; i<lot && it.hasNext(); i++){
				d = it.next();
				r.append(",");
				r.append("(").append(d.getIdDocument()).append(",'").append(d.getNomDocument()).append("')");
			}
		}	
		
		try {
			this.connect.createStatement().executeUpdate(r.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}

	@Override
	public void delete(Document obj) {
		try {			
            this.connect
               	.createStatement()
               	.executeUpdate(
               			"DELETE FROM Document WHERE idDocument = " + obj.getIdDocument()
                );		
	    } catch (SQLException e) {
	            e.printStackTrace();
	    }
	}

	@Override
	public Document find(int id) {
		Document document = new Document();                
        
        try {
	        ResultSet result = this.connect
	                               .createStatement()
	                               .executeQuery(
	                                    "select * from Document "+
	                                    " where idDocument = "+ id
	                               );

            if(result.first()){
            	document = new Document(id, result.getString("nomDocument"));            	
            }
            else{ document = null; }
        } catch (SQLException e) {
                e.printStackTrace();
        }       
        return document;
	}
	
	@Override
	public Document update(Document obj) {
		try {			
            this .connect	
                 .createStatement()
                 .executeUpdate(
                	"UPDATE Document SET nomDocument = '" + obj.getNomDocument() + "'"+
                	" WHERE idDocument = " + obj.getIdDocument()
                 );
		
            obj = this.find(obj.getIdDocument());
	    } catch (SQLException e) {
	            e.printStackTrace();
	    }	    
	    return obj;
	}
	
	public int getIdDocByName(String docName){
		int idDoc = 0;
        
        try {        	
	        ResultSet result = this.connect
	                               .createStatement()
	                               .executeQuery(
	                                    "select * from Document "+
	                                    " where nomDocument = '"+ docName +"'"
	                               );

            if(result.first()){
            	idDoc = result.getInt("idDocument");            	
            }
        } catch (SQLException e) {
                e.printStackTrace();
        }       
        return idDoc;
	}
}
